<template>
  <div>
    <Header/>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input type="textarea" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <mavon-editor v-model="ruleForm.content"></mavon-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Header from "../components/Header";
export default {
  name: "BlogEdit",
  components:{Header},
  data(){
    return{
      ruleForm:{},
      rules:{
        title:{required:true,message:'标题不能为空',trigger:'blur'},
        description:{required:true,message:'描述不能为空',trigger:'blur'},
        content:{required:true,message:'内容不能为空',trigger:'blur'}
      }
    }
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid)=>{
        if (valid){
          this.$axios.post('/blog',this.ruleForm,{
            headers:{
              "Authorization":localStorage.getItem('token')
            }
          }).then(res=>{
            console.log(res);
            this.$alert('保存成功','提示',{
              confirmButtonText:'确定',
              callback: action => {
                this.$router.push("/blog")
              }
            })
          })
        }
        else{
          console.log('1111');
          return false
        }
      });
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const blogId = this.$route.params.blogId;
    if (blogId){
      this.$axios.get('/blog/'+blogId).then(res => {
        const blog = res.data.data;
        this.ruleForm.id = blog.id;
        this.ruleForm.title = blog.title;
        this.ruleForm.description = blog.description;
        this.ruleForm.content = blog.content;
        console.log(this.ruleForm);
      })
    }
  }
}
</script>

<style scoped>

</style>
